﻿using EventHandlerDemo.Domain.Bases;
using EventHandlerDemo.Domain.OrderAttachments;
using EventHandlerDemo.Domain.Orders;

namespace EventHandlerDemo.Application.Orders;

public interface IOrderAppService
{
    Task<int> CreateaAsync();
}

public class OrderAppService : IOrderAppService
{
    private readonly IOrderRepository _orderRepository;
    private readonly IOrderAttachmentRepository _orderAttachmentRepository;
    private readonly IUnitOfWork _unitOfWork;

    public OrderAppService(
        IOrderRepository orderRepository,
        IOrderAttachmentRepository orderAttachmentRepository,
        IUnitOfWork unitOfWork)
    {
        _orderRepository = orderRepository;
        _orderAttachmentRepository = orderAttachmentRepository;
        _unitOfWork = unitOfWork;
    }

    public async Task<int> CreateaAsync()
    {
        var order = new Order()
        {
            Name = Guid.NewGuid().ToString(),
        };
        await _orderRepository.InsertAsync(order);
        await _unitOfWork.SaveChangeAsync();

        var orderAttachment = new OrderAttachment()
        {
            OrderId = order.Id,
            Description = Guid.NewGuid().ToString(),
            CreationTime = DateTime.Now,
        };
        await _orderAttachmentRepository.InsertAsync(orderAttachment);

        return order.Id;
    }
}